package com.wf.mock.app.http;


import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import com.alibaba.fastjson.JSONObject;

/**
 * Created by Administrator on 13-12-17.
 */
public class HttpRequest {
	final static int BUFFER_SIZE = 4096;
	final static String Tag = "RequestHttp";

	private static String addToken(String url) {
		String token = "088B839419ADC6AF95B670300EF9836D6A29276B989135F9B39D4C8D8472093C";
		// 如果url中已经包括"?"
		String paramStr = "?";
		if (url.contains("?")) {
			paramStr = "&";
		}
		if (!url.contains("token=")) {
			url += paramStr + "token=" + token;
		}
		// url += "&lang=" + LanguageSetting.getCurrLangStr();
		return url;
	}

	/*
	 * POST
	 * 
	 * @param url
	 * 
	 * @return
	 * 
	 * @throws Exception
	 */
	@SuppressWarnings("deprecation")
	public static InputStream POSTJson(String url, String json)
			throws Exception {
		url = addToken(url);
		HttpPost request = new HttpPost(url);
		HttpEntity entity = new StringEntity(json, "UTF-8");
		request.setEntity(entity);
		request.addHeader("Content-Type", "application/json;charset=UTF-8");
		HttpParams httpParameters = new BasicHttpParams();
		JSONObject jsonObject = JSONObject.parseObject(json);
		httpParameters.setParameter("domain",jsonObject.getString("domain"));
		httpParameters.setParameter("userId",jsonObject.getString("userId"));
		httpParameters.setParameter("content",jsonObject.getString("content"));
		httpParameters.setParameter("recipients",jsonObject.getString("recipients"));
		httpParameters.setParameter("wavDuration",jsonObject.getString("wavDuration"));
		int timeoutConnection = 5000;
		HttpConnectionParams.setConnectionTimeout(httpParameters,
				timeoutConnection);
		int timeoutSocket = 5000;
		HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
		HttpClient client = new DefaultHttpClient(httpParameters);
		HttpResponse response = client.execute(request);

		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			return response.getEntity().getContent();
		}
		return null;
	}
	
	
	public static InputStream POST(String url, JSONObject json)
			throws IOException, InvocationTargetException,
			NoSuchMethodException, InstantiationException,
			IllegalAccessException {
		List<NameValuePair> p = new ArrayList<NameValuePair>();
		StringBuffer sb = new StringBuffer();
		Set entrySet = json.keySet();
		for (Object key : entrySet) {
			Object value = json.get(key);
			if (key != null && value != null) {
				p.add(new BasicNameValuePair(key.toString(), value
						.toString()));
			}
			sb.append(key + "=" + value + "&");
		}
		
		HttpPost httpPost = null;
		httpPost = new HttpPost(addToken(url));
		HttpEntity entity = new UrlEncodedFormEntity(p, "UTF-8");
		httpPost.setEntity(entity);
		System.out.println(url + "?" + sb.toString());
		HttpClient client = HttpsUtil.getNewHttpClient();
		HttpResponse response = client.execute(httpPost);
//		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//			return response.getEntity().getContent();
//		}
		return null;
	}
	
	

	public static void main(String[] args) throws Exception {
		
		String url = "http://127.0.0.1/helper/msg/sendMsg";
		String json = "{\"domain\":\"wafertest.com\",\"userId\":\"guojh@wafertest.com\",\"content\":\"test-=-------http---------\",\"recipients\":\"guochao\",\"wavDuration\":\"0\",\"msgType\":\"10\"}";
		for (int i = 0; i < 100; i++) {
			Thread.sleep(10);
			JSONObject jsonObject = JSONObject.parseObject(json);
			jsonObject.put("content",
					jsonObject.getString("content") +" order:"+ i +" :sendTime:" + System.currentTimeMillis());
			POST(url, jsonObject);
		}
	}

}
